﻿package away3d.core.managers ;
/**
 * Author: David Lenaerts
 */


import away3d.materials.utils.MipmapGenerator ;
import flash.display.BitmapData ;
import flash.display3D.Context3D ;
import flash.display3D.Context3DTextureFormat ;
import flash.display3D.textures.Texture ;



class Texture3DProxy
{
    
    
    private var _bitmapData     : BitmapData ;
    private var _textures       : Vector<Texture> ;
    private var _dirty          : Vector<Bool> ;
    private var _maxIndex       : Int ;
    private var _mipMapTex      : BitmapData ;
    
    
    
    public function Texture3DProxy( ?mipMapTex : BitmapData = null )
    {
        _maxIndex   = -1 ;
        _mipMapTex  = mipMapTex ;
        _textures   = new Vector( 8 ) ;
        _dirty      = new Vector( 8 ) ;
    }
    
    
    private function get_bitmapData() : BitmapData
    {
        
        return _bitmapData ;
        
    }
    
    
    private function set_bitmapData( value : BitmapData )
    {
        
        if( value == _bitmapData ) return ;
        
        if( _bitmapData ) 
        {
            
            if( value.width != _bitmapData.width || value.height != _bitmapData.height )
            {
                
                invalidateSize() ;
                
            }
            else
            {
                
                invalidateContent() ;
                
            }
            
        }
        
        _bitmapData = value ;
        
    }
    
    
    public function invalidateContent()
    {
        
        for( i in 0..._maxIndex )
        {
            
            _dirty[ i ] = true ;
            
        }
        
    }
    
    
    private function invalidateSize()
    {
        
        var tex : Texture ;
        
        for ( i in 0..._maxIndex )
        {
            
            tex = _textures[ i ] ;
            if( tex ) 
            {
                
                tex.dispose() ;
                _textures[ i ]  = null ;
                _dirty[ i ]     = false ;
                
            }
            
        }
        
    }
    
    
    public function dispose( deep : Bool )
    {
        if( deep && _bitmapData )
        {
            
            _bitmapData.dispose() ;
            _bitmapData = null ;
            
        }
        
        for ( i in 0..._maxIndex )
        {
            
            if( _textures[ i ] ) _textures[ i ].dispose() ;
            
        }
    }
    
    
    private function get_TextureForContext( context : Context3D, contextIndex : Int ) : Texture
    {
        
        if( contextIndex > _maxIndex ) _maxIndex = contextIndex ;
        
        var tex = _textures[ contextIndex ] ;
        
        if( !tex || _dirty[ contextIndex ] )
        {
            
            if( !tex ) _textures[ contextIndex ] = tex = context.createTexture( _bitmapData.width, _bitmapData.height, Context3DTextureFormat.BGRA, false ) ;
            
            MipmapGenerator.generateMipMaps( _bitmapData, tex, _mipMapTex, true ) ;
            
            _dirty[ contextIndex ] = false ;
            
        }
        
        return tex ;
        
    }
    
    
}
